package com.erp.order.purchasing.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.order.purchasing.dao.QuotedPriceDao;
import com.erp.order.purchasing.model.QuotedPrice;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class QuotedPriceDaoImpl extends BaseDaoImpl<QuotedPrice> implements QuotedPriceDao {
    @Autowired
    private DaoSupport daoSupport;

    @Override
    public QuotedPrice getDataObject(String s) {
        return null;
    }

    @Override
    public List<QuotedPrice> getDataObjects(QuotedPrice quotedPrice) {
        String sql = "SELECT il.* FROM quoted_price il WHERE il.inquiry_list_id = :id";
        Map<String, Object> args = new HashMap<>();
        args.put("id", quotedPrice.getInquiryListId());
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("il", QuotedPrice.class);
        return this.daoSupport.selectDataSql(sql, entity, args);
    }

    @Override
    public List<QuotedPrice> getDataObjectsForDataAuth(String s, Pages pages, QuotedPrice quotedPrice) {
        return null;
    }

    @Override
    public QuotedPrice addCompleteData(QuotedPrice quotedPrice) {
        this.daoSupport.insertDataTransaction(quotedPrice);
        return quotedPrice;
    }

    @Override
    public List<QuotedPrice> getData(String id,String username) {
        String sql = "SELECT il.* FROM quoted_price il WHERE il.status=1 " +
                     "AND il.inquiry_list_id = :id " +
                     "AND il.vendor_id=(SELECT m.vendor_id FROM md_vendor m WHERE m.credit_code=:username)";
        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        args.put("username", username);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("il", QuotedPrice.class);
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
}
